package com.rongshu.sendmessage.fragment.util;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.rongshu.sendmessage.R;

import java.util.List;

/**
 * 往期开奖数据适配器
 */
public class AddImageAdapter extends RecyclerView.Adapter<AddImageAdapter.ViewHolder> {

    private List<String> dataArr;
    private OnRecycleViewItemClickListener listener;

    public AddImageAdapter(List<String> dataArr,OnRecycleViewItemClickListener listener) {
        this.dataArr = dataArr;
        this.listener = listener;
    }

    public void setList(List<String> list) {
        this.dataArr = list;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup recyclerView, int viewType) {
        Context context = recyclerView.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.menu_grid_item, recyclerView, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        View itemView = holder.itemView;
        SimpleDraweeView item_tv = itemView.findViewById(R.id.item_sv);
        ImageView iv_delete = itemView.findViewById(R.id.iv_delete);
        String image = dataArr.get(position);
        if (image.equals(R.mipmap.new_add_photo + "")) {
            item_tv.setImageResource(R.mipmap.new_add_photo);
            iv_delete.setVisibility(View.GONE);
        } else {
            iv_delete.setVisibility(View.VISIBLE);
            String pic = dataArr.get(position);
            item_tv.setImageURI(pic);
        }
        if (listener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    listener.OnItemClick(v,position);
                }
            });
            iv_delete.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    listener.OnItemClick(v,position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {

        return dataArr.size();
    }


    public interface OnRecycleViewItemClickListener {
        void OnItemClick(View view, int position);
    }
}
